import {
  query as queryUsers,
/*   employelist,
  employeDetail,
  removeemploye,
  updateEmploye,
  createmploye,
  queryEmploye, */
  jigou,
  zuser,
  cuser,
  tableuser
} from '../services/data';

export default {
  namespace: 'data',

  state: {
    currentJigou: [],
    currentZuser:[],
    currentCuser:[],
    currentTableuser:[],
    currentEmploye: [],
    currentDetail: {},
  },

  effects: {
    //人员信息列表
    *getJigou(_, { call, put }) {
      const response = yield call(jigou);
      /* console.log(response); */
      yield put({
        type: 'saveCurrentJigou',
        payload: response
      });
    },
    *getZuser(_, { call, put }) {
      const response = yield call(zuser);
      yield put({
        type: 'saveCurrentZuser',
        payload: response
      });
    },
    *getCuser(_, { call, put }) {
      const response = yield call(cuser);
      /* console.log(response); */
      yield put({
        type: 'saveCurrentCuser',
        payload: response
      });
    },
    *getTableuser(_, { call, put }) {
      const response = yield call(tableuser);
      /* console.log(response); */
      yield put({
        type: 'saveCurrentTableuser',
        payload: response
      });
    },
    //表单关键字查询
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryEmploye, payload);
    },
    //增加人员信息
    *create({ payload: values }, { call, put }) {
      const response = yield call(createmploye, values);
      console.log(response);
      yield put({ type: 'getEmployelist' });
      if (response != '') {
        yield put(routerRedux.push('/employe/employelist'));
      } 
    },
    //删除人员信息
    *remove({ payload: id }, { call, put }) {
      yield call(removeemploye, id);
      yield put({ type: 'getEmployelist' });
    },
    //查详情信息
    *employedetail({ payload: id }, { call, put }) {
      const response = yield call(employeDetail, id);
      yield put({
        type: 'saveDetailEmploye',
        payload: response,
      });
    },
    //修改人员信息
    *update({ payload: { id, values } }, { call, put }) {
      yield call(updateEmploye, id, values);
      yield put({ type: 'getEmployelist' });
    },
  },

  reducers: {
    saveCurrentJigou(state, action) {
      return {
        ...state,
        currentJigou: action.payload,
      };
    },
    saveCurrentZuser(state, action) {
      return {
        ...state,
        currentZuser: action.payload,
      };
    },
    saveCurrentCuser(state, action) {
      return {
        ...state,
        currentCuser: action.payload,
      };
    },
    saveCurrentTableuser(state, action) {
      return {
        ...state,
        currentTableuser: action.payload,
      };
    },
    saveDetailEmploye(state, action) {
      return {
        ...state,
        currentDetail: action.payload || {},
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentJigou: {
          ...state.currentJigou,
          notifyCount: action.payload,
        },
        currentZuser: {
          ...state.currentZuser,
          notifyCount: action.payload,
        },
        currentCuser: {
          ...state.currentCuser,
          notifyCount: action.payload,
        },
        currentTableuser: {
          ...state.currentTableuser,
          notifyCount: action.payload,
        },
        currentDetail: {
          ...state.currentDetail,
          notifyCount: action.payload,  
        },
      };
    },
  },
};
